package de.fhhof.drkarl;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

import de.drkarl.bots.questionbot.fragenKatalog.Antwort;
import de.drkarl.bots.questionbot.fragenKatalog.Frage;
import de.drkarl.bots.questionbot.fragenKatalog.FragenKatalog;

public class QuestionParser {
	
	public static void main(String[] args) {
		try {
			FragenKatalog katalog = new FragenKatalog();
			FileReader f = new FileReader("questions.txt");
			BufferedReader br = new BufferedReader(f);
			String s = null;
			String question = null;
			String answer = null;
			while((s = br.readLine()) != null){
				
				String sr[] = s.split(":");
				boolean flag = false;
				if(sr[0].equals("Question")){
					question = sr[1];
					
				}else if(sr[0].equals("Answer")){
					answer = sr[1];
					
					flag = true;
				}
				if(flag){
					boolean flag2 = true;
					if(flag2){
						for (Frage frage : katalog.getFragen()) {
							if(frage.frage.equals(question)){
								System.out.println("Ich kenne Frage: "+frage.frage);
								System.out.println("Adde Atwort: "+answer);
								System.out.println("-------------");
								Antwort antwort = new Antwort(answer);
								frage.addAntwort(antwort);
								flag2 = false;
							}
						}
					}
					if(flag2){
					LinkedList<Antwort> antworten = new LinkedList<Antwort>();
					antworten.addLast(new Antwort(answer));
					Frage frage = new Frage(question, antworten);
					katalog.addFrage(frage);
					System.out.println("Question: "+question);
					System.out.println("Answer: "+answer);
					}
				}
				flag = false;
				//Thread.sleep(300);
			}
			System.out.println("fertig");
			System.out.println(katalog.toXML());
			katalog.writeToXML();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	
	}
}
